package com.atguigu.gulimall.search.app;

import com.atguigu.common.exception.BizCodeEnum;
import com.atguigu.common.to.SkuSearchVo;
import com.atguigu.common.utils.R;
import com.atguigu.gulimall.search.service.ProductService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/search/save")
@Slf4j
public class ElasticSearchSaveController {

    @Autowired
    private ProductService productService;

    @PostMapping("/product")
    public R saveProduct(@RequestBody List<SkuSearchVo> skuSearchVos){
        boolean hasFailures;
        try {
            hasFailures = productService.up(skuSearchVos);
        } catch (IOException e) {
            log.error(this.getClass().getName().toString()+"商品上架异常");
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }

        if (hasFailures){
            log.error(this.getClass().getName().toString()+"商品上架异常，传入参数有误,参数为{}",skuSearchVos.toString());
            return R.error(BizCodeEnum.PRODUCT_UP_EXCEPTION.getCode(),BizCodeEnum.PRODUCT_UP_EXCEPTION.getMsg());
        }else {
            return R.ok();
        }
    }



}
